﻿namespace Games.Light
{
    class Game
    {
        private bool[,] status = new bool[1, 1];
        private byte[,] click = new byte[1, 1];
        public int Count { get; private set; }

        public void Init(byte width, byte height)
        {
            this.width = width;
            this.height = height;
            this.status = new bool[width, height];
            this.click = new byte[width, height];
            this.Count = 0;
        }

        public void Click(int x, int y)
        {
            this.click[x, y]++;
            this.Change(x, y);
            this.Change(x + 1, y);
            this.Change(x, y + 1);
            this.Change(x - 1, y);
            this.Change(x, y - 1);
        }
        public bool InBoard(int x, int y)
        {
            if(x < 0 || x >= width)
            {
                return false;
            }
            if(y < 0 || y >= height)
            {
                return false;
            }
            return true;
        }
        private void Change(int x, int y)
        {
            if(InBoard(x, y))
            {
                if(status[x, y])
                {
                    status[x, y] = false;
                    this.Count--;
                }
                else
                {
                    status[x, y] = true;
                    this.Count++;
                }
            }
        }

        private byte width;
        private byte height;

        public readonly byte Min = 3, Max = 15;

        public bool IsFinished
        {
            get
            {
                return this.Count == this.width * this.height;
            }
        }

        public bool IsSize(byte width, byte height)
        {
            return this.width == width && this.height == height;
        }

        public int GetClick()
        {
            var c1 = 0;
            for(byte i = 0; i < width; i++)
            {
                for(byte j = 0; j < height; j++)
                {
                    if(this.click[i, j] % 2 == 1)
                    {
                        c1++;
                    }
                }
            }
            return c1;
        }
    }
}
